<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" class="form-inline">
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleAddUser">新增</el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-search" @click="handleSearchUserList">查询</el-button>
      </el-form>

      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="userName" label="用户名" width="200"/>
        <el-table-column label="授权平台" width="">
          <template slot-scope="scope">
            <span v-for="platform in scope.row.platformList" :key="platform">
              <el-tag type="success" style="margin-left: 5px">{{ platform }}</el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEditUser(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDeleteUser(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :visible.sync="editFormVisible" :title="editFormTitle">
        <el-form ref="editForm" :model="tempUser" :rules="rules" label-position="left" label-width="70px">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="tempUser.userName" placeholder="用户名" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="tempUser.password" type="password" placeholder="密码" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="授权平台">
            <el-checkbox-group v-model="tempUser.platformList">
              <el-checkbox v-for="item in platformListOptions" :key="item.name" :label="item.name" style="width: 150px;margin-left: 0px"/>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editFormVisible = false">取 消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="handleSaveUser">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { searchPlatformList, searchUserList, saveUser, deleteUser } from '@/api/settings.js'
export default {
  name: 'Settings',
  directives: { waves },
  components: { },
  data() {
    return {
      fullscreenLoading: false,
      editFormVisible: false,
      editFormTitle: '',
      userList: [],
      platformListOptions: [],
      tempUser: {
        userName: '',
        password: '',
        platformList: []
      },
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    searchPlatformList().then(response => {
      if (response.code === '0000') {
        this.platformListOptions = response.data
      }
    })
    this.handleSearchUserList()
  },
  methods: {
    handleSearchUserList() {
      searchUserList().then(response => {
        if (response.code === '0000') {
          this.userList = response.data
        }
      })
    },
    handleEditUser(row) {
      this.editFormVisible = true
      this.editFormTitle = '编辑用户'
      this.tempUser.userName = row.userName
      this.tempUser.password = row.password
      this.tempUser.platformList = row.platformList
    },
    handleAddUser() {
      this.editFormVisible = true
      this.editFormTitle = '新增用户'
      this.tempUser.userName = ''
      this.tempUser.password = ''
      this.tempUser.platformList = []
    },
    handleDeleteUser(row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tempUser.userName = row.userName
        this.fullscreenLoading = true
        deleteUser(this.tempUser).then(response => {
          if (response.code === '0000') {
            this.$alert('删除用户成功!', '', {
              confirmButtonText: '确定',
              callback: action => {
                this.handleSearchUserList()
              }
            })
          } else {
            this.$message.error('error:' + response.msg)
          }
          this.fullscreenLoading = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSaveUser() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.fullscreenLoading = true
          saveUser(this.tempUser).then(response => {
            if (response.code === '0000') {
              this.$alert('保存用户成功!', '', {
                confirmButtonText: '确定',
                callback: action => {
                  this.handleSearchUserList()
                }
              })
              this.editFormVisible = false
            } else {
              this.$message.error('error:' + response.msg)
            }
            this.fullscreenLoading = false
          })
        }
      })
    }
  }
}
</script>
<style>
</style>

